package mn.more.wits.server.util;

import org.apache.log4j.Logger;

/**
 * general-purpose validation utility.  Should be used ONLY on the server-side.
 *
 * @author <a href="mailto:mike.liu@aptechmongolia.edu.mn">Mike Liu</a>
 * @version $Id: ValidationUtil.java 8 2008-09-02 00:40:02Z mikeliucc $
 */
public final class ValidationUtil {

	// Per requirement: 6 to 25 character in length, alphanumeric, UNICODE, may include (._-@)
	public static final String JS_REGEX_USERNAME = "([\\" + "u0040\\" + "u002D\\" + "u002e\\" +
	                                               "u0030-\\" + "u0039\\" + "u0041-\\" + "u005a\\" +
	                                               "u005f\\" +
	                                               "u0061-\\" + "u007a\\" +
	                                               "u0400-\\" + "uffff]{6,25})";
	// Per requirement: Must not begin or end with spaces.
	public static final String JS_REGEX_USERNAME2 = "^[^\\s]+.*[^\\s]+$";
	// Per requirements: 8 to 25 characters in length, alphanumeric, UNICODE, punctuations and spaces
	public static final String JS_REGEX_PASSWORD = "([\\" + "u0020-\\" + "u007e\\" + "u0400-\\" + "uffff]{8,25})";
	// Per requirement: Must not begin or end with spaces.
	public static final String JS_REGEX_PASSWORD2 = "^[^\\s]+.*[^\\s]+$";
	// Per requirements: at least one punctuation and one number.
	// todo: the REAL regex is turned off to ease development and testing.
	// todo: But it REALLY should be turned back on for real app. use/testing!
	public static final String JS_REGEX_PASSWORD3 = "(.*?)([\\d].*?[\\" + "u0021-\\" + "u002f\\" +
	                                                "u003a-\\" + "u0040\\" + "u005b-\\" + "u0060]|[\\" +
	                                                "u0021-\\" + "u002f\\" + "u003a-\\" + "u0040\\" +
	                                                "u005b-\\" + "u0060].*?[\\d])(.*)";
	//public static final String JS_REGEX_PASSWORD3 = ".*";

	private ValidationUtil() { }

	/**
	 * verify that <code>data</code> is not null nor empty (length() < 1).  Other logger will be used to print out such
	 * error.
	 *
	 * @param name   The context name of <code>data</code>
	 * @param data   The text to be tested
	 * @param logger The logger to be used for printing.  If null, nothing will be printed out.
	 */
	public static boolean isBlank(String name, String data, Logger logger) {
		if (data == null) {
			if (logger != null) { if (logger.isDebugEnabled()) { logger.debug("null " + name + " found."); } }
			return true;
		}

		if (data.length() < 1) {
			if (logger != null) { if (logger.isDebugEnabled()) { logger.debug("empty " + name + " found."); } }
			return true;
		}

		// todo: use AppMsg to output i18n message 
		return false;
	}

	public static boolean isValidUsername(String data, Logger logger) {
		if (ValidationUtil.isBlank("username", data, logger)) { return false; }

		boolean test1 = data.matches(JS_REGEX_USERNAME);
		if (!test1) {
			if (logger.isDebugEnabled()) { logger.debug("username validation failed on " + JS_REGEX_USERNAME); }
			return false;
		}

		boolean test2 = data.matches(JS_REGEX_USERNAME2);
		if (!test2) {
			if (logger.isDebugEnabled()) { logger.debug("username validation failed on " + JS_REGEX_USERNAME2); }
			return false;
		}

		return true;
	}
}
